package com.punai.evaluationSystem.incomeDataEvalute.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageHelper;
import com.punai.common.constant.HttpStatus;
import com.punai.common.core.page.PageDomain;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.core.page.TableSupport;
import com.punai.common.utils.NumberUtil;
import com.punai.common.utils.PageUtils;
import com.punai.common.utils.StringUtil;
import com.punai.config.domain.ConfigInventoryChapter;
import com.punai.config.domain.ConfigInventoryLib;
import com.punai.config.mapper.ConfigInventoryChapterMapper;
import com.punai.config.mapper.ConfigInventoryLibMapper;
import com.punai.dataclean.mapper.IncomeDataCleanMapper;
import com.punai.dataclean.service.IncomeDataCleanService;
import com.punai.evaluationSystem.costDataEvalute.bo.CostDataEvaluteBo;
import com.punai.evaluationSystem.costDataEvalute.domain.EvaluationResults;
import com.punai.evaluationSystem.incomeDataEvalute.bo.IncomeDataEvaluteBo;
import com.punai.evaluationSystem.incomeDataEvalute.domain.EvaluationDataPriceSr;
import com.punai.evaluationSystem.incomeDataEvalute.domain.EvaluationResultsSr;
import com.punai.evaluationSystem.incomeDataEvalute.domain.EvaluationSrInfo;
import com.punai.evaluationSystem.incomeDataEvalute.mapper.EvaluationDataPriceSrMapper;
import com.punai.evaluationSystem.incomeDataEvalute.mapper.EvaluationResultsSrMapper;
import com.punai.evaluationSystem.incomeDataEvalute.mapper.EvaluationSrInfoMapper;
import com.punai.evaluationSystem.incomeDataEvalute.service.IncomeLrEvaluteService;
import com.punai.externalccb.domain.EcoCbfeeitem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 收入数据评价
 */
@Service
@Transactional
public class IncomeLrEvaluteServiceImpl implements IncomeLrEvaluteService {
    @Autowired
    private  EvaluationResultsSrMapper evaluationResultsSrMapper;
    @Autowired
    private IncomeDataCleanMapper incomeDataCleanMapper;
    @Autowired
    private ConfigInventoryChapterMapper configInventoryChapterMapper;
    @Autowired
    private ConfigInventoryLibMapper configInventoryLibMapper;
    @Autowired
    private EvaluationDataPriceSrMapper evaluationDataPriceSrMapper;
    @Autowired
    private EvaluationSrInfoMapper evaluationSrInfoMapper;
    @Override
    public List<EvaluationResultsSr> initResultsSrTable(IncomeDataEvaluteBo incomeDataEvaluteBo) {
        IncomeDataEvaluteBo convert = IncomeDataEvaluteBo.convert(incomeDataEvaluteBo);
        PageHelper.startPage(convert.getPageNum(),convert.getPageSize());
        List<EvaluationResultsSr> resultsList = evaluationResultsSrMapper.selectEvaluationResultsSr(new IncomeDataEvaluteBo() {{
            setInventoryStandard(convert.getInventoryStandard());
            setClassification(convert.getEvaluteType());
            setEvaluteType("01");
            setProjectDept(convert.getProjectDept());
            setAddress(convert.getAddress());
            setPriceType(convert.getPriceType());
            if (CollUtil.isNotEmpty(convert.getDateRange())){
                setTimeStart(convert.getDateRange().get(0));
                setTimeEnd(convert.getDateRange().get(1));
            }
        }});

        return resultsList;
    }

    @Override
    public int saveAssetLibrary(List<String> resultIds) {
        int i = evaluationResultsSrMapper.updateResultsSrByIdIn(resultIds,"1");
        return i;
    }

    @Override
    public List<IncomeDataEvaluteBo> initCleanDataTable(IncomeDataEvaluteBo incomeDataEvaluteBo) {
        List<IncomeDataEvaluteBo> res = null;
        IncomeDataEvaluteBo bo = IncomeDataEvaluteBo.convert(incomeDataEvaluteBo);
        Long companyId = bo.getCompanyId();
        String categoryId = bo.getCategoryId();
        String type = bo.getType();
        List<String> priceTypes = new ArrayList<>();
        if(bo.getPriceType()!=null && bo.getPriceType().equals("全部")){
            bo.setPriceType(null);
            for (Map<String, String> map : IncomeDataCleanService.IncomeProjectTypeKV) {
                priceTypes.add(map.get("key"));
            }
        }else{
            priceTypes.add(bo.getPriceType());
        }
        if (CollUtil.isNotEmpty(bo.getDateRange())){
            bo.setTimeStart(bo.getDateRange().get(0));
            bo.setTimeEnd(bo.getDateRange().get(1));
        }
        if(type.equals("fb_0")){ //分部分项清单
            res = new ArrayList<>();
            List<String> chapterIds = new ArrayList<>();
            chapterIds.add(categoryId);
            List<ConfigInventoryChapter> children = configInventoryChapterMapper.selectChildById(categoryId, companyId);
            if (CollUtil.isNotEmpty(children)) {
                for (ConfigInventoryChapter child : children) {
                    chapterIds.add(child.getId());
                }
            }
            List<ConfigInventoryLib> all = configInventoryLibMapper.selectConfigInventoryLibList(new ConfigInventoryLib() {{
                setCompanyId(bo.getCompanyId());
                setListNorm(bo.getInventoryStandard());
                setType(bo.getIncentoryType());
            }}, chapterIds);
            List<String> codes = all.stream().map(ConfigInventoryLib::getListCoding).collect(Collectors.toList());
            if(codes.size()==0) return res;
            bo.setCodes(codes);
        }
        List<IncomeDataEvaluteBo> incomeDataEvaluteBos = evaluationResultsSrMapper.selectCleanData(bo);
        return incomeDataEvaluteBos;
    }

    @Override
    public TableDataInfo initCleanDataEvaluteTable(IncomeDataEvaluteBo evaluteBobo) {
        IncomeDataEvaluteBo incomeDataEvaluteBo = IncomeDataEvaluteBo.convert(evaluteBobo);
        List<IncomeDataEvaluteBo> res = null;
        Long companyId = incomeDataEvaluteBo.getCompanyId();
        String categoryId = incomeDataEvaluteBo.getCategoryId();
        String type = incomeDataEvaluteBo.getType();
        List<String> idList = Arrays.asList(incomeDataEvaluteBo.getIdStr().split(",", -1));
        if(type.equals("fb_0")) { //分部分项清单
            res = new ArrayList<>();
            List<String> chapterIds = new ArrayList<>();
            chapterIds.add(categoryId);
            List<ConfigInventoryChapter> children = configInventoryChapterMapper.selectChildById(categoryId, companyId);
            if (CollUtil.isNotEmpty(children)) {
                for (ConfigInventoryChapter child : children) {
                    chapterIds.add(child.getId());
                }
            }
            List<ConfigInventoryLib> all = configInventoryLibMapper.selectConfigInventoryLibList(new ConfigInventoryLib() {{
                setCompanyId(incomeDataEvaluteBo.getCompanyId());
                setListNorm(incomeDataEvaluteBo.getInventoryStandard());
                setType(incomeDataEvaluteBo.getIncentoryType());
            }}, chapterIds);
            List<String> codes = all.stream().map(ConfigInventoryLib::getListCoding).collect(Collectors.toList());
            if(codes.size()!=0) incomeDataEvaluteBo.setCodes(codes);
            incomeDataEvaluteBo.setCodes(codes);
            if (CollUtil.isNotEmpty(idList)){
                for (String id : idList) {
                    List<String> idAndAddress = Arrays.asList(id.split("=", -1));
                    if (CollUtil.isEmpty(idAndAddress)) continue;
                    incomeDataEvaluteBo.setId(idAndAddress.get(0));
                    incomeDataEvaluteBo.setAddress(idAndAddress.get(1));
                    incomeDataEvaluteBo.setPageSize(null);
                    List<IncomeDataEvaluteBo> boList = evaluationResultsSrMapper.selectCleanDataEvaluteList(incomeDataEvaluteBo);
                    if (CollUtil.isNotEmpty(boList)){
                        IncomeDataEvaluteBo bo = new IncomeDataEvaluteBo();
                        bo.setId(boList.get(0).getId());
                        bo.setListCode(boList.get(0).getListCode());
                        bo.setListName(boList.get(0).getListName());
                        bo.setXmtz(boList.get(0).getXmtz());
                        bo.setAddress(boList.get(0).getAddress());
                        bo.setJldw(boList.get(0).getJldw());
                        bo.setGznr(boList.get(0).getGznr());
                        bo.setJsgz(boList.get(0).getJsgz());
                        List<BigDecimal> priceList = boList.stream().filter(v -> v.getPrice() != null).map(IncomeDataEvaluteBo::getPrice).collect(Collectors.toList());
                        if (CollUtil.isNotEmpty(priceList)){
                            BigDecimal zhdjSum = priceList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
                            BigDecimal max = priceList.stream().max(Comparator.comparing(x -> x)).orElse(null).setScale(3,BigDecimal.ROUND_HALF_UP);
                            BigDecimal min = priceList.stream().min(Comparator.comparing(x -> x)).orElse(null).setScale(3,BigDecimal.ROUND_HALF_UP);
                            bo.setZhdj(zhdjSum!=null ? zhdjSum.setScale(3,BigDecimal.ROUND_HALF_UP) : null);
                            bo.setPriceRange(min +"-" +max);
                            List<EcoCbfeeitem> cbFeeItemList = new ArrayList<>();
                            for (IncomeDataEvaluteBo dataEvaluteBo : boList) {
                                String linkDataJson = dataEvaluteBo.getLinkDataJson();
                                List<EcoCbfeeitem> ecoCbfeeitems = JSON.parseArray(linkDataJson, EcoCbfeeitem.class);
                                if (CollUtil.isNotEmpty(ecoCbfeeitems)) cbFeeItemList.addAll(ecoCbfeeitems);
                            }
                            if (CollUtil.isNotEmpty(cbFeeItemList)){
                                BigDecimal srj = cbFeeItemList.stream().filter(v -> v.getYsprice() != null).map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                                BigDecimal cbj = cbFeeItemList.stream().filter(v -> v.getTargettaxprice() != null).map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                                BigDecimal lre = NumberUtil.subtractNew(srj, cbj);
                                BigDecimal lrl = NumberUtil.divideNew(lre, 4, srj);
                                bo.setLre(lre);
                                bo.setLrl(lrl);
                            }
                        }
                        res.add(bo);
                    }
                }
            }
        }else {
            res = new ArrayList<>();
            if (CollUtil.isNotEmpty(idList)){
                for (String id : idList) {
                    List<String> idAndAddress = Arrays.asList(id.split("=", -1));
                    if (CollUtil.isEmpty(idAndAddress)) continue;
                    incomeDataEvaluteBo.setId(idAndAddress.get(0));
                    incomeDataEvaluteBo.setAddress(idAndAddress.get(1));
                    incomeDataEvaluteBo.setPageSize(null);
                    List<IncomeDataEvaluteBo> boList = evaluationResultsSrMapper.selectCleanDataEvaluteList(incomeDataEvaluteBo);
                    if (CollUtil.isNotEmpty(boList)){
                        IncomeDataEvaluteBo bo = new IncomeDataEvaluteBo();
                        bo.setId(boList.get(0).getId());
                        bo.setListCode(boList.get(0).getListCode());
                        bo.setListName(boList.get(0).getListName());
                        bo.setXmtz(boList.get(0).getXmtz());
                        bo.setAddress(boList.get(0).getAddress());
                        bo.setJldw(boList.get(0).getJldw());
                        bo.setGznr(boList.get(0).getGznr());
                        bo.setJsgz(boList.get(0).getJsgz());
                        List<BigDecimal> priceList = boList.stream().filter(v -> v.getPrice() != null).map(IncomeDataEvaluteBo::getPrice).collect(Collectors.toList());
                        if (CollUtil.isNotEmpty(priceList)){
                            BigDecimal zhdjSum = priceList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
                            BigDecimal max = priceList.stream().max(Comparator.comparing(x -> x)).orElse(null).setScale(3,BigDecimal.ROUND_HALF_UP);
                            BigDecimal min = priceList.stream().min(Comparator.comparing(x -> x)).orElse(null).setScale(3,BigDecimal.ROUND_HALF_UP);
                            bo.setZhdj(zhdjSum!=null ? zhdjSum.setScale(3,BigDecimal.ROUND_HALF_UP) : null);
                            bo.setPriceRange(min +"-" +max);
                            List<EcoCbfeeitem> cbFeeItemList = new ArrayList<>();
                            for (IncomeDataEvaluteBo dataEvaluteBo : boList) {
                                String linkDataJson = dataEvaluteBo.getLinkDataJson();
                                List<EcoCbfeeitem> ecoCbfeeitems = JSON.parseArray(linkDataJson, EcoCbfeeitem.class);
                                if (CollUtil.isNotEmpty(ecoCbfeeitems)) cbFeeItemList.addAll(ecoCbfeeitems);
                            }
                            if (CollUtil.isNotEmpty(cbFeeItemList)){
                                BigDecimal srj = cbFeeItemList.stream().filter(v -> v.getYsprice() != null).map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                                BigDecimal cbj = cbFeeItemList.stream().filter(v -> v.getTargettaxprice() != null).map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                                BigDecimal lre = NumberUtil.subtractNew(srj, cbj);
                                BigDecimal lrl = NumberUtil.divideNew(lre, 4, srj);
                                bo.setLre(lre);
                                bo.setLrl(lrl);
                            }
                        }
                        res.add(bo);
                    }
                }
            }
        }
        PageDomain pageDomain = TableSupport.buildPageRequest();
        int pageSize = pageDomain.getPageSize();
        int start = (pageDomain.getPageNum()-1) * pageSize;
        int end = start + pageSize;
        if (end>res.size()) end = res.size();
        List<IncomeDataEvaluteBo> list = res.subList(start, end);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(res.size());
        return rspData;
    }

    @Override
    public List<IncomeDataEvaluteBo> initTableDetailInfoData(IncomeDataEvaluteBo incomeDataEvaluteBo) {
        if (incomeDataEvaluteBo.getPriceType()!=null && incomeDataEvaluteBo.getPriceType().equals("全部")){
            incomeDataEvaluteBo.setPriceType(null);
        }
        List<IncomeDataEvaluteBo> res = null;
        List<String> idAndAddress = Arrays.asList(incomeDataEvaluteBo.getIdStr().split("=", -1));
        if (CollUtil.isNotEmpty(idAndAddress)){
            incomeDataEvaluteBo.setId(idAndAddress.get(0));
            incomeDataEvaluteBo.setAddress(idAndAddress.get(1));
        }
        if (StringUtil.isNotNullOrEmpty(incomeDataEvaluteBo.getType()) && incomeDataEvaluteBo.getType().equals("view")){
            res = evaluationSrInfoMapper.initTableDetailInfoData(incomeDataEvaluteBo);
        }else {
            res = evaluationResultsSrMapper.initTableDetailInfoData(incomeDataEvaluteBo);
        }
        return res;
    }

    @Override
    public List<String> cleanDataInfoIdList(IncomeDataEvaluteBo bo) {
        IncomeDataEvaluteBo incomeDataEvaluteBo = IncomeDataEvaluteBo.convert(bo);
        List<String> ids = new ArrayList<>();
        List<String> addressList = new ArrayList<>();
        List<String> idAndAddressList = Arrays.asList(incomeDataEvaluteBo.getIdStr().split(",", -1));
        for (String id : idAndAddressList) {
            List<String> idAndAddress = Arrays.asList(id.split("=", -1));
            if (CollUtil.isEmpty(idAndAddress)) continue;
            ids.add(idAndAddress.get(0));
            addressList.add(idAndAddress.get(1));
        }
        incomeDataEvaluteBo.setIds(ids);
        incomeDataEvaluteBo.setCodes(addressList);
        incomeDataEvaluteBo.setPageSize(null);
        List<IncomeDataEvaluteBo> boList = evaluationResultsSrMapper.initTableDetailInfoData(incomeDataEvaluteBo);
        return boList.stream().map(IncomeDataEvaluteBo::getId).collect(Collectors.toList());
    }

    @Override
    public IncomeDataEvaluteBo recalculation(IncomeDataEvaluteBo incomeDataEvaluteBo) {
        if (incomeDataEvaluteBo.getPriceType()!=null && incomeDataEvaluteBo.getPriceType().equals("全部")){
            incomeDataEvaluteBo.setPriceType(null);
        }
        IncomeDataEvaluteBo bo = new IncomeDataEvaluteBo();
        List<String> singleFeeId = Arrays.asList(incomeDataEvaluteBo.getSingleFeeId().split("=", -1));
        List<String> ids = Arrays.asList(incomeDataEvaluteBo.getIdStr().split(",", -1));
        if (CollUtil.isNotEmpty(singleFeeId)){
            String id = singleFeeId.get(0);
            String address = singleFeeId.get(1);
            bo.setId(id);
            bo.setAddress(address);
            incomeDataEvaluteBo.setId(id);
            incomeDataEvaluteBo.setAddress(address);
            incomeDataEvaluteBo.setOids(ids);
            List<IncomeDataEvaluteBo> boList = null;
            if (StringUtil.isNotNullOrEmpty(incomeDataEvaluteBo.getType()) && incomeDataEvaluteBo.getType().equals("view")){
                boList = evaluationSrInfoMapper.initTableDetailInfoData(incomeDataEvaluteBo);
            }else {
                boList = evaluationResultsSrMapper.selectCleanDataEvaluteList(incomeDataEvaluteBo);
            }
            if (CollUtil.isNotEmpty(boList)){
                bo.setListCode(boList.get(0).getListCode());
                bo.setListName(boList.get(0).getListName());
                bo.setXmtz(boList.get(0).getXmtz());
                bo.setJldw(boList.get(0).getJldw());
                bo.setGznr(boList.get(0).getGznr());
                bo.setJsgz(boList.get(0).getJsgz());
                List<BigDecimal> priceList = boList.stream().filter(v -> v.getPrice() != null).map(IncomeDataEvaluteBo::getPrice).collect(Collectors.toList());
                if (CollUtil.isNotEmpty(priceList)){
                    BigDecimal zhdjSum = priceList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal max = priceList.stream().max(Comparator.comparing(x -> x)).orElse(null).setScale(3,BigDecimal.ROUND_HALF_UP);
                    BigDecimal min = priceList.stream().min(Comparator.comparing(x -> x)).orElse(null).setScale(3,BigDecimal.ROUND_HALF_UP);
                    bo.setZhdj(zhdjSum!=null ? zhdjSum.setScale(3,BigDecimal.ROUND_HALF_UP) : null);
                    bo.setPriceRange(min +"-" +max);
                    List<EcoCbfeeitem> cbFeeItemList = new ArrayList<>();
                    for (IncomeDataEvaluteBo dataEvaluteBo : boList) {
                        String linkDataJson = dataEvaluteBo.getLinkDataJson();
                        List<EcoCbfeeitem> ecoCbfeeitems = JSON.parseArray(linkDataJson, EcoCbfeeitem.class);
                        if (CollUtil.isNotEmpty(ecoCbfeeitems)) cbFeeItemList.addAll(ecoCbfeeitems);
                    }
                    if (CollUtil.isNotEmpty(cbFeeItemList)){
                        BigDecimal srj = cbFeeItemList.stream().filter(v -> v.getYsprice() != null).map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                        BigDecimal cbj = cbFeeItemList.stream().filter(v -> v.getTargettaxprice() != null).map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                        BigDecimal lre = NumberUtil.subtractNew(srj, cbj);
                        BigDecimal lrl = NumberUtil.divideNew(lre, 4, srj);
                        bo.setLre(lre);
                        bo.setLrl(lrl);
                    }
                }
            }
        }
        return bo;
    }

    @Override
    public int saveEvaluationPrice(IncomeDataEvaluteBo incomeDataEvaluteBo) {
        int insert;
        if (incomeDataEvaluteBo.getPriceType()!=null && incomeDataEvaluteBo.getPriceType().equals("全部")){
            incomeDataEvaluteBo.setPriceType(null);
        }
        String titleName = incomeDataEvaluteBo.getName();
        List<IncomeDataEvaluteBo> saveData = incomeDataEvaluteBo.getSaveData();
        List<String> ids = incomeDataEvaluteBo.getIds();//明细id
        Map<BigDecimal, List<IncomeDataEvaluteBo>> btjgsl = saveData.stream().filter(v->v.getZhdj()!=null).collect(Collectors.groupingBy(IncomeDataEvaluteBo::getZhdj, Collectors.toList()));
        //保存结果表
        EvaluationResultsSr resultsSr = new EvaluationResultsSr();
        resultsSr.setIsassets(0);//不存在资产库
        resultsSr.setEvaluateType("01");
        resultsSr.setClassification(incomeDataEvaluteBo.getType());
        resultsSr.setPjjgsj(titleName);
        resultsSr.setCount(saveData.size());
        resultsSr.setBtjgsl(btjgsl.size());
        resultsSr.setPriceType(incomeDataEvaluteBo.getInventoryStandard());
        insert = evaluationResultsSrMapper.insert(resultsSr);
        //保存数据明细表
        if (CollUtil.isNotEmpty(saveData)){
            for (IncomeDataEvaluteBo saveDatum : saveData) {
                EvaluationDataPriceSr dataPriceSr = new EvaluationDataPriceSr();
                EvaluationDataPriceSr convert = EvaluationDataPriceSr.convert(dataPriceSr, saveDatum);
                convert.setPid(resultsSr.getId());
                insert = evaluationDataPriceSrMapper.insert(convert);
                if (CollUtil.isNotEmpty(ids)){
                    List<IncomeDataEvaluteBo> boList = evaluationResultsSrMapper.selectCleanDataEvaluteList(new IncomeDataEvaluteBo(){{
                        setAddress(saveDatum.getAddress());
                        setId(saveDatum.getId());
                        setOids(ids);
                    }});
                    List<EvaluationSrInfo> convertSrInfo = EvaluationSrInfo.convert(convert.getId(), boList);
                    if (CollUtil.isNotEmpty(convertSrInfo)){
                        for (EvaluationSrInfo info : convertSrInfo) {
                            insert = evaluationSrInfoMapper.insert(info);
                        }
                    }
                }
            }
        }
        return insert;
    }

    @Override
    public int updateEvaluationPrice(IncomeDataEvaluteBo incomeDataEvaluteBo) {
        int i;
        String resultId = incomeDataEvaluteBo.getId();
        String titleName = incomeDataEvaluteBo.getName();
        List<IncomeDataEvaluteBo> saveData = incomeDataEvaluteBo.getSaveData();
        List<String> ids = incomeDataEvaluteBo.getIds();//明细id
        Map<BigDecimal, List<IncomeDataEvaluteBo>> btjgsl = saveData.stream().filter(v->v.getZhdj()!=null).collect(Collectors.groupingBy(IncomeDataEvaluteBo::getZhdj, Collectors.toList()));
        //修改结果表
        EvaluationResultsSr resultsSr = evaluationResultsSrMapper.selectEvaluationResultsSrById(resultId);
        resultsSr.setPjjgsj(titleName);
        resultsSr.setBtjgsl(btjgsl.size());
        resultsSr.setCount(saveData.size());
        i = evaluationResultsSrMapper.updateById(resultsSr);
        //删除未选中的数据
        List<EvaluationDataPriceSr> dataPriceSrList = evaluationDataPriceSrMapper.selectEvaluationDataPriceSrList(new EvaluationDataPriceSr() {{
            setPid(resultId);
        }});
        List<String> dataPriceSrIds = saveData.stream().map(IncomeDataEvaluteBo::getId).collect(Collectors.toList());
        List<String> deleteDataIds = dataPriceSrList.stream().filter(v -> !dataPriceSrIds.contains(v.getId())).map(EvaluationDataPriceSr::getId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(deleteDataIds)){
            i = evaluationDataPriceSrMapper.deleteBatchIds(deleteDataIds);
        }
        //修改 数据明细
        for (IncomeDataEvaluteBo saveDatum : saveData) {
            EvaluationDataPriceSr dataPriceSr = evaluationDataPriceSrMapper.selectEvaluationDataPriceSrById(saveDatum.getId());
            dataPriceSr.setZhdj(saveDatum.getZhdj());
            dataPriceSr.setLre(saveDatum.getLre());
            dataPriceSr.setLrl(saveDatum.getLrl());
            dataPriceSr.setPriceRange(saveDatum.getPriceRange());
            dataPriceSr.setEvaluationDate(new Date());
            i = evaluationDataPriceSrMapper.updateById(dataPriceSr);
            if (CollUtil.isNotEmpty(ids)){
                List<EvaluationSrInfo> evaluationSrInfos = evaluationSrInfoMapper.selectEvaluationSrInfoList(new EvaluationSrInfo() {{
                    setPid(saveDatum.getId());
                }});
                List<String> infoIds = evaluationSrInfos.stream().filter(v->!ids.contains(v.getOid())).map(EvaluationSrInfo::getId).collect(Collectors.toList());
                if (CollUtil.isNotEmpty(infoIds)){
                    i = evaluationSrInfoMapper.deleteBatchIds(infoIds);
                }
            }
        }
        return i;
    }

    @Override
    public List<EvaluationDataPriceSr> initEvaluteDataList(IncomeDataEvaluteBo incomeDataEvaluteBo) {
        List<EvaluationDataPriceSr> res = new ArrayList<>();
        Long companyId = incomeDataEvaluteBo.getCompanyId();
        String categoryId = incomeDataEvaluteBo.getCategoryId();
        String type = incomeDataEvaluteBo.getTabType();
        List<String> codes = new ArrayList<>();
        if(type.equals("fb_0")) { //分部分项清单
            List<String> chapterIds = new ArrayList<>();
            chapterIds.add(categoryId);
            List<ConfigInventoryChapter> children = configInventoryChapterMapper.selectChildById(categoryId, companyId);
            if (CollUtil.isNotEmpty(children)) {
                for (ConfigInventoryChapter child : children) {
                    chapterIds.add(child.getId());
                }
            }
            List<ConfigInventoryLib> all = configInventoryLibMapper.selectConfigInventoryLibList(new ConfigInventoryLib() {{
                setCompanyId(incomeDataEvaluteBo.getCompanyId());
                setListNorm(incomeDataEvaluteBo.getInventoryStandard());
                setType(incomeDataEvaluteBo.getIncentoryType());
            }}, chapterIds);
            codes = all.stream().map(ConfigInventoryLib::getListCoding).collect(Collectors.toList());
        }
        PageUtils.startPage();
        List<EvaluationDataPriceSr> list = evaluationDataPriceSrMapper.selectEvaluationDataPriceSrList(new EvaluationDataPriceSr() {{
            setCode(incomeDataEvaluteBo.getListCode());
            setName(incomeDataEvaluteBo.getListName());
            setPid(incomeDataEvaluteBo.getId());
        }});
        if (CollUtil.isNotEmpty(codes)){
            List<String> finalCodes = codes;
            list = list.stream().filter(v -> finalCodes.contains(v.getCode())).collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public List<String> evaluteDataInfoIdList(IncomeDataEvaluteBo incomeDataEvaluteBo) {
        List<IncomeDataEvaluteBo> boList = evaluationSrInfoMapper.initTableDetailInfoData(new IncomeDataEvaluteBo() {{
            setPid(incomeDataEvaluteBo.getId());
        }});
        return boList.stream().map(IncomeDataEvaluteBo::getId).collect(Collectors.toList());
    }
}
